package com.fingard.avatar.website.config;

import com.fingard.avatar.website.exception.BizException;
import com.fingard.avatar.website.model.po.SysUserPo;

/**
 * @author 徐一涛
 * @date 2022/4/26
 */
public class UserContext {
    private static final ThreadLocal<SysUserPo> LOCAL_USER = new ThreadLocal<>();

    private UserContext() {}

    public static SysUserPo getUser() {
        SysUserPo baseUser = LOCAL_USER.get();
        if (null == baseUser) {
            throw BizException.of("登录失效，请重新登录！");
        }
        return baseUser;
    }

    public static Long getUserId() {
        return getUser().getId();
    }

    public static void setUser(SysUserPo user) {
        LOCAL_USER.set(user);
    }

    public static void remove() {
        LOCAL_USER.remove();
    }

}
